package com.baoyouqun.controller.back;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baoyouqun.domain.Query.LeopardFriendPageQuery;
import com.baoyouqun.domain.UpdateCmd.UserInfoSettingDTO;
import com.baoyouqun.domain.UpdateCmd.UserUpdateCmd;
import com.baoyouqun.domain.VO.CellphoneDuplicateVO;
import com.baoyouqun.domain.VO.LeopardFriendDetailVO;
import com.baoyouqun.domain.VO.LeopardFriendImportVO;
import com.baoyouqun.domain.VO.UserVO;
import com.baoyouqun.domain.cmd.BatchUserStatusCmd;
import com.baoyouqun.domain.cmd.SingleUserStatusCmd;
import com.baoyouqun.service.LeopardFriendManageService;
import com.baoyouqun.service.LeopardFriendService;
import com.baoyouqun.service.UserService;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 重复豹友管理独立Controller（仅依赖新Service处理核心查询/导出）
 */
@RestController
@RequestMapping("/api/leopard-friends/independent")
public class LeopardFriendIndependentController {

    // 新Service：处理分页、导出、手机号校验
    @Resource
    private LeopardFriendManageService leopardFriendManageService;

    // 原有Service：处理导入、启用禁用（职责隔离）
    @Resource
    private LeopardFriendService leopardFriendService;

    @Resource
    private UserService userService;

    /**
     * 1. 分页搜索（现在依赖新Service）
     */
    @GetMapping("/page")
    @ApiOperation("豹友分页查询（新）")
    public SingleResponse<IPage<LeopardFriendDetailVO>> pageLeopardFriendsBack(LeopardFriendPageQuery query) {
        return leopardFriendManageService.pageLeopardFriend(query);
    }

    /**
     * 2. 单个启用/禁用（保留原有Service）
     */
    @PutMapping("/status/single")
    @ApiOperation("单个豹友启用/禁用")
    public Response updateSingleStatus(@RequestBody SingleUserStatusCmd cmd) {
        return leopardFriendService.updateSingleUserStatus(cmd);
    }

    /**
     * 3. 批量启用/禁用（保留原有Service）
     */
    @PutMapping("/status/batch")
    @ApiOperation("批量豹友启用/禁用")
    public Response updateBatchStatus(@RequestBody BatchUserStatusCmd cmd) {
        return leopardFriendService.updateBatchUserStatus(cmd);
    }

    /**
     * 完善个人信息（不变）
     */
    @PostMapping("/setUserInfo")
    @ApiOperation("完善个人基本信息")
    public Response setUserInfo(@Valid @RequestBody UserInfoSettingDTO dto) {
        UserUpdateCmd userInfo = dto.getUserUpdateCmd();
        return userService.setUserInfo(userInfo, dto.getResourceList());
    }

    /**
     * 用户信息（不变）
     */
    @RequestMapping("getUserInfo")
    @ApiOperation("用户信息查询")
    public SingleResponse<UserVO> getUserInfo(String userId) {
        return SingleResponse.of(userService.userDetail(userId));
    }

    /**
     * 5. 导入（保留原有Service）
     */
    @PostMapping("/import")
    @ApiOperation("豹友批量导入")
    public SingleResponse<LeopardFriendImportVO> importLeopardFriends(@RequestParam("file") MultipartFile file) throws IOException {
        return leopardFriendService.importLeopardFriends(file);
    }

    /**
     * 6. 导出（现在依赖新Service）
     */
    @GetMapping("/export")
    @ApiOperation("豹友批量导出（新）")
    public void exportLeopardFriends(LeopardFriendPageQuery query, HttpServletResponse response) throws IOException {
        leopardFriendManageService.exportLeopardFriend(query, response);
    }

    /**
     * 7. 检查手机号重复（现在依赖新Service）
     */
    @GetMapping("/check-duplicate")
    @ApiOperation("手机号重复检查（新）")
    public SingleResponse<CellphoneDuplicateVO> checkDuplicate(@RequestParam String cellphone) {
        return leopardFriendManageService.checkCellphoneDuplicate(cellphone);
    }
}